package com.magicode.cms.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IInvoiceDao;
import com.magicode.cms.entiy.base.Invoice;
import com.magicode.cms.service.IInvoiceService;

@Service("serviceinvoice")
public class InvoiceServiceImpl extends BaseServiceImpl<Invoice, IInvoiceDao> implements IInvoiceService,UtilPath{

	@Autowired
	@Qualifier("daoinvoice")
	private IInvoiceDao invoiceDao;
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "日期");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "recipient");
		map.put(json_title, "用户名");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "单位名称");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "address");
		map.put(json_title, "地址（邮编）");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "mobile");
		map.put(json_title, "手机");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "moneycount");
		map.put(json_title, "发票金额");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "express");
		map.put(json_title, "快递单号");
		map.put("width", 100);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql =" from "+Invoice.class.getName()+" "+entity_alias_name+" ";
			
		if(null!=querywhere && !querywhere.isEmpty()){
			
			if(querywhere.containsKey(field_id)){
				hql = hql+" where "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		List<Map<String, Object>> resultList = null;
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("createTime", ""+entity_alias_name+".createTime as createTime");
			gridsel.put("address", ""+entity_alias_name+".address as address");
			gridsel.put("mobile", ""+entity_alias_name+".mobile as mobile");
			gridsel.put("money", ""+entity_alias_name+".money as money");
			gridsel.put("expressOrderNo", ""+entity_alias_name+".expressOrderNo as expressOrderNo");
			gridsel.put("title", ""+entity_alias_name+".title as title");
			gridsel.put("recipient", ""+entity_alias_name+".recipient as recipient");
			gridsel.put("expressCompany", ""+entity_alias_name+".expressCompany as expressCompany");
			//gridsel.put("shareexpress", ""+entity_alias_name+"|| '(' || .expressCompany || ')' || "+entity_alias_name+".expressOrderNo "
			//		+ "  as shareexpress ");

			resultList = this.queryMapDataList(hql, gridsel, start, size);
			for (Map<String, Object> map : resultList) {
				String expressOrderNo=(String) map.get("expressOrderNo");
				String expressCompany=(String) map.get("expressCompany");
				BigDecimal money= (BigDecimal) map.get("money");
				 BigDecimal count = money.setScale(2, BigDecimal.ROUND_HALF_UP);
				map.put("moneycount", count);
				if(null!=expressCompany){
					map.put("express","("+ expressCompany+")"+expressOrderNo+"");
				}
			}
			return resultList;
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id)  {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		String [] fetchJoins = new String[2];
		fetchJoins[0] = "type";
		fetchJoins[1] = "status";
		Invoice invoice=null;
		try {
			invoice = getObject(id, fetchJoins);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(invoice.getStatus().getCode().equals("express")||invoice.getStatus().getCode().equals("finished")){
			opt.setName("已投递");
			opt.setUrl("#");
			opt.setType("edit");
			opts.add(opt);
		}else{
			opt.setName("投递");
			opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url+"?status=1");
			opt.setType("edit");
			opts.add(opt);
			
		}
		
		return opts;
	}
	
	
	
	@Override
	public IInvoiceDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return invoiceDao;
	}

	public IInvoiceDao getInvoiceDao() {
		return invoiceDao;
	}

	public void setInvoiceDao(IInvoiceDao invoiceDao) {
		this.invoiceDao = invoiceDao;
	}

	
}
